﻿using System.ComponentModel;
using System.Data;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Xml;

namespace AccessWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public Employee emp;
        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = this;

            MyArrayList = new List<string>();

            MyArrayList.Add("aaa");
            MyArrayList.Add("bbb");


            //this.listBoxEmplyee.ItemsSource = new List<Employee>() { new Employee() { Id = 1, Name = "Tim", Age = 30 }, new Employee() { Id = 2, Name = "Tom", Age = 26 }, new Employee() { Id = 3, Name = "Guo", Age = 26 } };
            //this.listBoxEmplyee.DisplayMemberPath = "Name";//显示每条的哪个属性，toString 放到TextBlock，再包装进ListBoxItem
            //this.listBoxEmplyee.SelectedValuePath = "Id";//调取SelectedValue属性时，找Id这个字段的值
            //itemscontrol
            //ComboBox   ContextMenu   ListBox   ListView   Menu  StatusBar    TabControl    TreeView  
            //HeaderedItemsControl
            //MenuItem   TreeViewItem   ToolBar

            //emp = new Employee();
            //Binding binding = new Binding();
            //binding.Source = emp;
            //binding.Path = new PropertyPath("Name");
            //BindingOperations.SetBinding(this.textBoxName,TextBox.TextProperty,binding);
            //or
            this.textBoxName.SetBinding(TextBox.TextProperty, new Binding("Nam") { Source = emp = new Employee() });

            //集合的第一个元素作为默认元素
            this.textBoxs1.SetBinding(TextBox.TextProperty, new Binding("/") { Source = stringList });
            this.textBoxs2.SetBinding(TextBox.TextProperty, new Binding("/Length") { Source = stringList, Mode = BindingMode.OneWay });
            this.textBoxs3.SetBinding(TextBox.TextProperty, new Binding("/[2]") { Source=stringList,Mode = BindingMode.OneWay });

            //集合作为源时，多级的绑定    error
            this.textBoxc1.SetBinding(TextBox.TextProperty, new Binding("/Name") { Source = countryList,Mode=BindingMode.OneWay });
            this.textBoxc2.SetBinding(TextBox.TextProperty, new Binding("/ProvinceList.Name") { Source = countryList, Mode = BindingMode.OneWay });
            this.textBoxc3.SetBinding(TextBox.TextProperty, new Binding("/ProvinceList/CityList.Name") { Source = countryList, Mode = BindingMode.OneWay });


            stuList.Add(new Student() { Id = 0, Name = "Tim" });
            stuList.Add(new Student() { Id = 1, Name = "Tom" });
            stuList.Add(new Student() { Id = 2, Name = "Kyle" });
            this.listBoxStudents114.ItemsSource = stuList;
            this.listBoxStudents114.DisplayMemberPath = "Name";
            this.textBoxId114.SetBinding(TextBox.TextProperty,new Binding("SelectedItem.Id") { Source=this.listBoxStudents114, Mode = BindingMode.OneWay });


            this.listBoxStudents116.ItemsSource = stuList;
            //this.listBoxStudents116.DisplayMemberPath = "Name";
            this.textBoxId116.SetBinding(TextBox.TextProperty, new Binding("SelectedItem.Id") { Source = this.listBoxStudents116, Mode = BindingMode.OneWay });

            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            dt.Columns.Add("Age");
            for (int i = 0; i < 10; i++)
            {
                DataRow dr = dt.NewRow();
                dr["ID"] = i;
                dr["Name"] = "Tim" + i.ToString();
                dr["Age"] = 20 + i;
                dt.Rows.Add(dr);
            }



        }

        private List<string> arrayList;

        public List<string> MyArrayList
        {
            get { return arrayList; }
            set { arrayList = value; }
        }

        public static string WindowTitle = "山高月小";
        public static string ShowText { get { return "水落石出"; } }

        private void buttonVictor_Click(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            var level1 = VisualTreeHelper.GetParent(btn);
            var level2 = VisualTreeHelper.GetParent(level1);
            var level3 = VisualTreeHelper.GetParent(level2);

            MessageBox.Show(level1.GetType().ToString());//contentpresenter
            MessageBox.Show(level2.GetType().ToString());//border
            MessageBox.Show(level3.GetType().ToString());//ListBoxItem
        }

        public class Employee :INotifyPropertyChanged
        {
            public int Id { get; set; }
            private string name;

            public string Nam
            {
                get { return name; }
                set {
                    name = value;
                    if(this.PropertyChanged != null)
                    {
                        this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Nam"));
                    }
                }
            }

            public int Age { get; set; }

            public event PropertyChangedEventHandler? PropertyChanged;
        }

        private void buttonTextBox_Click(object sender, RoutedEventArgs e)
        {
            emp.Nam += "Name2222";
        }

        public List<string> stringList { get; set; } = new List<string>() { "Tim", "Tom", "Blog" };


        public class Country
        {
            public string Name { get; set; }
            public List<Province> ProvinceList { get; set; }
        }
        public class Province
        {
            public string Name { get; set; }
            public List<City> CityList { get; set; }
        }
        public class City
        {
            public string Name { get; set; }
        }
        public List<Country> countryList { get; set; } = new List<Country>() { new Country() { Name = "中国", ProvinceList = new List<Province>() { new Province() { Name = "四川", CityList = new List<City>() { new City() { Name = "成都" } } } } } };

        private void buttonDataContext_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(this.btnDataContext.DataContext.ToString());
        }

        public List<Student> stuList { get; set; } = new List<Student>();

        public DataTable dt {  get; set; } = new DataTable();

        private void buttonListBoxViewData117_Click(object sender, RoutedEventArgs e)
        {
            this.listBoxStudents117.DisplayMemberPath = "Name";
            this.listBoxStudents117.ItemsSource = dt.DefaultView;//Dataview 类型实现了Ienumerable接口
            
        }

        private void buttonListViewData118_Click(object sender, RoutedEventArgs e)
        {
            this.listView118.ItemsSource = dt.DefaultView;
            //or
            //this.listView118.DataContext = dt;
            //this.listView118.SetBinding(ListView.ItemsSourceProperty, new Binding());

            //this.listView118.ItemsSource = dt;  // 错误写法 无法转换
        }

        private void buttonListViewData119_Click(object sender, RoutedEventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            var path = AppDomain.CurrentDomain.BaseDirectory;
            var docPath = Path.Combine(path, "Resources", "Xmls", "RawData119.xml");
            doc.Load(docPath);

            XmlDataProvider xdp = new XmlDataProvider();
            xdp.Document = doc;

            xdp.XPath = @"/StudentList/Student";

            this.listView119.DataContext = xdp;
            this.listView119.SetBinding(ListView.ItemsSourceProperty, new Binding());
        }

    }

    public partial class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}